<?php
	/**
	 * 
	 * 用户登陆系统
	 * @author xie.noah
	 *
	 */
	class RegisterAction extends AgriculturalproductsCommonAction {
		
			public function index() {
				$this->display();
			}
		
		/**
		 *
		 * 验证码生成
		 */
		public function verify(){
			$type =	isset($_GET['type'])?$_GET['type']:'gif';
			import("@.ORG.Image");
			Image::buildImageVerify(4,1,$type);
		}
		
		/**
		 * 
		 * 提交注册信息
		 */
		public function postinfo(){
			if($_SESSION['verify'] != md5($_POST['verify'])) {
				$this->error('验证码错误！');
			}
			$model = D ("loginuser");
			if (false === $model->create ()) {
				$this->success ( $model->getError () );
			}
			//保存当前数据对象
			$data ["name"] = $_POST['username'];
			$data ["pwd"] = $_POST['password'];
			$data ["email"] = $_POST['email'];
			$data ["phone"] = $_POST['phone'];
			$data ["createdate"] = date('Y-m-d H:i:s',time());
			$data ["status"] = 1;
			$lastInsId = $model->add($data);
			if ($lastInsId != -1) {
				//注册后跳转至首页
				$this->assign("jumpUrl","__APP__/Index/");
				//设置session
				Session::set(C('USER_AUTH_KEY'),$_POST['username']);
				//Session::set('username',$_POST['username']);
				$this->success ( "新增用户成功" );
			}else{
				$this->success($model->getError ()) ;
			}
		}
		
		/**
		 * 用户注销
		 * Enter description here ...
		 */
		public function loginout(){


			if(Session::is_set(C('USER_AUTH_KEY'))){
				//Session::destroy() ;
				unset($_SESSION[C('USER_AUTH_KEY')]);
				$this->assign("jumpUrl","__APP__/");
				$this->success('登出成功！');
			}else{
				$this->success('已经登出！');
			}

		}
		
		/**
		 * 
		 * 密码验证
		 */
		public function pwsvalidate(){
			$pwd = $_REQUEST["password"];
			$pwdagain = $_REQUEST["passwordagain"];
			if($pwd!=$pwdagain){
				echo("两次密码输入不一致，请重新输入!");
			}else{
				echo "success";
			}
		}

		public function uservalidate(){
			$user = $_REQUEST["username"];
			if($user == ""){
				echo "用户名不能为空！";
				return;
			}
			$model = D ( "loginuser" );
			// 构造查询条件
			$condition['name'] = $user;
			// 查询数据
			$list = $model->where($condition)->find();
			if(count($list)>0){
				//$this->ajaxReturn ( "111111", "已存在相同的用户，请选择其它用户名进行注册!", 1 );
				echo("已存在相同的用户，请选择其它用户名进行注册!");
			}else{
				//$this->ajaxReturn ( "111111", "可以使用这个用户进行注册!", 1 );
				//echo("可以使用这个用户进行注册!");
				echo "success";
			}
		}
	}
?>